package com.tdwyz.fictionmanage.service;

import com.tdwyz.fictionmanage.mapper.UserMapper;
import com.tdwyz.fictionmanage.pojo.Result;
import com.tdwyz.fictionmanage.pojo.User;
import com.tdwyz.fictionmanage.utils.JwtUtil;
import com.tdwyz.fictionmanage.utils.Md5Util;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author 14543
 * @version 1.0
 * @description: TODO
 * @date 2024/7/28 10:34
 */
@Service
public class UserService {

    @Autowired
    private UserMapper userMapper;

    @Resource
    private StringRedisTemplate template;

    /**
     * 登录
     * @param username
     * @param password
     * @return
     */
    public Result login(String username, String password) {
        //1、用户存在
        User user = userMapper.selectByUsername(username);
        if(user==null){
            return Result.defeat("用户不存在");
        }
        //2、校验密码 用户输入的密码与盐值加密后的密文 与 表里面的密文对比（MD5）
        //JWT
        System.out.println(Md5Util.getMD5String(password+user.getSalt()));
        if(Md5Util.getMD5String(password+user.getSalt()).equals(user.getPassword())){
            //用户信息载体
            HashMap<String,Object> claims = new HashMap<>();
            claims.put("id",user.getId());
            claims.put("username",user.getUsername());
            //生成令牌
            String token = JwtUtil.genToken(claims);
            //存储令牌到redius里面 并且设置过期时间
            ValueOperations<String, String> operations = template.opsForValue();
            operations.set(token,token,3, TimeUnit.HOURS);
            //返回jwt令牌
            return Result.success(token);
        }
        return Result.defeat("密码错误");
    }

    /**
     * 用户注册
     *
     * @param username
     * @param password
     * @return
     */
    public Result register(String username, String password) {

        if (username == null || password == null){
            return Result.defeat("用户名或密码为空！！！");
        }
        if (password.length() < 6 || password.length() > 18){
            return Result.defeat("密码长度应在6-188为之间");
        }
        User user = userMapper.selectByUsername(username);//根据用户名查询用户
        if (user != null) {
            return Result.defeat("用户已存在");
        }
        //密码加密
        String uuid = UUID.randomUUID().toString();
        password = Md5Util.getMD5String(password + uuid);
        //插入数据
        int add = userMapper.add(username, password, uuid);
        if (add == 0){
            return Result.defeat("注册失败");
        }
        return Result.success("注册成功");
    }

    public int update(User user) {
        return userMapper.update(user);
    }

    public int updateAvatar(String username, String avatarUrl) {
        return userMapper.updateAvatar(username,avatarUrl);
    }

    public int updatePwd(String newPwd, String username, String salt) {
        //加密
        newPwd=Md5Util.getMD5String(newPwd+salt);
        return userMapper.updatePwd(newPwd,username);
    }
}
